angular.module("mainapp",[])
    .controller("maincontroller",function($scope){
        $scope.inputName = "";
        $scope.inputPassword = "";
        
        $scope.setCookie = function(name,value){
			var cookie = [name, '=', JSON.stringify(value)].join('');
			document.cookie = cookie;
		}
        
        $scope.getCookie = function(name){
			var result = document.cookie.match(new RegExp(name + '=([^;]+)'));
			result && (result = JSON.parse(result[1]));
			return result;
		}
		
		$scope.deleteCookie = function(name) {
			  if ($scope.getCookie(name))
				  $scope.setCookie(name, "", -1);
		}
		
        //登录
        $scope.login = function(){
            if(checkFirst() != false){
                login_ajax($scope.inputName,$scope.inputPassword);
            }else{
            	 alert("请将信息填写完整...");
            };
        };
        function checkFirst(){
            if($scope.inputName!=null && $scope.inputName!=""
                && $scope.inputPassword!=null && $scope.inputPassword!=""){
                return true;
            }else{
                return false;
            }
        };
        function login_ajax(name,password){
            this.name = name;
            this.password = hex_md5(password);
            $.ajax({
                type:"POST",
                url:"/login/login",
                data:{"name":this.name,"password":this.password},
                contentType:"application/x-www-form-urlencoded",
                dataType:"json",
                success:function(data){
                    $scope.$apply(function(){
                    	 if(data.success == true && data.message == "登录成功"){
                        	$scope.inputAccount = "";$scope.inputPassword = "";
                        	location.href = '/';
                            //alert("登录成功1!");
                        }else if(data.success == false && data.message == "密码错误"){
                            $scope.inputAccount = "";$scope.inputPassword = "";
                            alert("密码错误!");
                        }else if(data.success == false && data.message == "该用户不存在"){
                            $scope.inputAccount = "";$scope.inputPassword = "";
                            alert("用户不存在!");
                        }
                    });
                }
            });
        };
    })

